package com.wang.API;

import com.wang.bean.Result;
import com.wang.bean.SysUserEntity;
import com.wang.config.ShiroUtils;
import com.wang.mapper.SysMenuMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * Shrio 用户登陆测试方法控制层
 */
@RestController
@RequestMapping("api")

public class ShiroController {
    private static Logger LOGGER = LoggerFactory.getLogger(ShiroController.class) ;

    @Resource
    private SysMenuMapper sysMenuMapper ;
    /**
     * 登录测试
     * http://localhost:7011/userLogin?userName=admin&passWord=admin
     */
    @RequestMapping("/userlogin")
    public Result userlogin (
            @RequestParam(value = "username") String username,
            @RequestParam(value = "password") String password, ModelMap map){
        Result result = new Result();
        try{
            Subject subject = ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);
            LOGGER.info("登录成功");
            result.setCode("100");
            map.addAttribute("token",subject.getSession().getId());
          //  map.addAttribute("getPrincipal",subject.getPrincipal());
            System.out.println(subject.getPrincipal());
            result.setData(map);
        }catch (Exception e) {
            LOGGER.info("未登陆成功");
            result.setCode("101");
           // e.printStackTrace();
        }
        return result;
    }



    /**
     * 获取当前用户信息
     * @param map
     * @return
     */
    @RequestMapping("/currentuserinfo")
    public Result userinfo (ModelMap map) {
        Result result = new Result();
        Subject subject = ShiroUtils.getSubject();
        SysUserEntity user= (SysUserEntity)subject.getPrincipal();
        if (user == null)
            result.setCode("108");
        else {
            result.setCode("105");
            user.setSalt("");
            user.setPassword("");
            result.setData(user);
        }
        return result;
    }

    /**
     * 服务器每次重启请求该接口之前必须先请求上面登录接口
     * http://localhost:7011/menu/list 获取所有菜单列表
     * 权限要求：sys:user:shiro
     */
    @RequestMapping("/menu/list")
    @RequiresPermissions("sys:user:shiro")
    public List list(){
        return sysMenuMapper.selectList() ;
    }

    /**
     * 用户没有该权限，无法访问
     * 权限要求：ccc:ddd:bbb
     */
    @RequestMapping("/menu/list2")
    @RequiresPermissions("ccc:ddd:bbb")
    public List list2(){
        return sysMenuMapper.selectList() ;
    }

    /**
     * 退出测试
     */
    @RequestMapping("/userLogOut1")
    public String logout (){
        ShiroUtils.logout();
        return "success" ;
    }
}